package com.zl.tby.screen.controller;

import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyUavInfoQuery;
import com.zl.tby.dto.TbyUavRecordQuery;
import com.zl.tby.model.TbyUavInfo;
import com.zl.tby.screen.service.ITbyMountainsScreenService;
import com.zl.tby.screen.vo.mountains.*;
import com.zl.tby.service.ITbyMountainsPersonService;
import com.zl.tby.service.ITbyUavInfoService;
import com.zl.tby.vo.TbyMountainsPersonForm;
import org.noear.solon.annotation.Body;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.validation.annotation.Valid;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 巡山护林大屏控制器
 * @author changpeng
 * @version 1.0
 * @since 2024/8/20 9:01
 */
@Valid
@Controller
@Mapping("/mountains")
public class TbyMountainsScreenController {

    @Inject
    private ITbyUavInfoService tbyUavInfoService;
    @Inject
    private ITbyMountainsScreenService tbyMountainsScreenService;
    @Inject
    private ITbyMountainsPersonService tbyMountainsPersonService;

    /**
     * 无人机列表
     * @return 列表数据
     */
    @Mapping(value = "/uav_list", method = MethodType.GET)
    public R<List<TbyUavInfo>> uavList() {
        return R.ok(this.tbyUavInfoService.list(new TbyUavInfoQuery()));
    }

    /**
     * 无人机详情
     * @return 无人机数据
     */
    @Mapping(value = "/uav_detail", method = MethodType.GET)
    public R<TbyUavInfo> uavDetail(Long id) {
        TbyUavInfo entity = this.tbyUavInfoService.get(id);
        if (entity != null) {
            entity.setFlightTime(ToolsUtil.divide(entity.getFlightTime(), new BigDecimal(3600), 1));
        }
        return R.ok(entity);
    }

    /**
     * 获取飞行数据
     * @param deviceId: 设备id
     * @param startDate：
     * @param endDate
     * @return
     */
    @Mapping(value = "/flight_data", method = MethodType.GET)
    public R<TbyMountainsScreenBean> flightData(String deviceId, String startDate, String endDate) {
        return R.ok(this.tbyMountainsScreenService.queryBackups(deviceId, startDate, endDate));
    }

    /**
     * 获取飞行数据汇总
     * @param query: 查询条件
     * @return 结果
     */
    @Mapping(value = "/query_flight_data", method = MethodType.GET)
    public R<TbyMountainsScreenBean> queryFlightData(@Body TbyUavRecordQuery query) {
        return R.ok(this.tbyMountainsScreenService.flightData(query));
    }

    /**
     * 获取飞行任务统计
     * @param query: 查询条件
     * @return 结果
     */
    @Mapping(value = "/query_flight_task_data", method = MethodType.GET)
    public R<List<TbyMountainsScreenBean>> queryFlightTaskData(@Body TbyUavRecordQuery query) {
        return R.ok(this.tbyMountainsScreenService.flightTaskData(query));
    }

    /**
     * 获取护林员列表信息
     * @return 结果
     */
    @Mapping(value = "/query_person_list", method = MethodType.GET)
    public R<List<TbyMountainsPerson>> queryPersonList() {
        return R.ok(this.tbyMountainsScreenService.personList());
    }

    /**
     * 获取护林员详情
     * @param id: 主键id
     * @return 结果
     */
    @Mapping(value = "/query_person_detail", method = MethodType.GET)
    public R<TbyMountainsPersonForm> queryPersonDetail(Long id) {
        TbyMountainsPersonForm entity = this.tbyMountainsPersonService.get(id);
        return entity == null ? R.failed("数据不存在") : R.ok(entity);
    }

    @Mapping(value = "/patrol_statistic", method = MethodType.GET)
    public R<TbyPatrolStatisticBean> patrolStatistic(String startTime, String endTime, Long userId) {
        return R.ok(this.tbyMountainsScreenService.patrolStatistic(startTime, endTime, userId));
    }

    @Mapping(value = "/patrol_statistic_group_user", method = MethodType.GET)
    public R<List<TbyPatrolStatisticBean>> patrolStatistic(String startTime, String endTime) {
        return R.ok(this.tbyMountainsScreenService.patrolStatisticGroupByUserId(startTime, endTime));
    }

    /**
     * 上报事件统计
     * @param startTime: 开始时间
     * @param endTime：结束时间
     * @return 结果
     */
    @Mapping(value = "/report_event", method = MethodType.GET)
    public R<TbyReportEventBean> reportEvent(String startTime, String endTime) {
        return R.ok(this.tbyMountainsScreenService.reportEvent(startTime, endTime));
    }

    /**
     * 上报事件列表
     * @param eventType: 事件类型
     * @param startTime：开始时间
     * @param endTime： 结束时间
     * @return 结果
     */
    @Mapping(value = "/report_event_list", method = MethodType.GET)
    public R<List<TbyReportEventInformation>> reportEventList(String eventType, String startTime, String endTime) {
        return R.ok(this.tbyMountainsScreenService.reportEventList(eventType, startTime, endTime));
    }

    /**
     * 获取上报事件附件id
     * @param guid: 事件与附件的关联标识
     * @return 结果
     */
    @Mapping(value = "/report_file_list", method = MethodType.GET)
    public R<List<TbyReportAttachBean>> reportFileList(String guid) {
        return R.ok(this.tbyMountainsScreenService.reportFileList(guid));
    }

    /**
     * 获取巡护轨迹
     * @param startTime：开始时间
     * @param endTime：结束时间
     * @return 轨迹列表
     */
    @Mapping(value = "/track", method = MethodType.GET)
    public R<List<List<Map<String, BigDecimal>>>> track(String startTime, String endTime) {
        return R.ok(this.tbyMountainsScreenService.track(startTime, endTime));
    }

    @Mapping(value = "/track_str", method = MethodType.GET)
    public R<List<Map<String, Object>>> trackStr(String startTime, String endTime) {
        return R.ok(this.tbyMountainsScreenService.trackStr(startTime, endTime));
    }

    /**
     * 获取实时飞行轨迹
     * @return 飞行轨迹
     */
    @Mapping(value = "/real_track", method = MethodType.GET)
    public R<Map<Long, TbyUavPointScreenVo>> realTrack() {
        return R.ok(this.tbyMountainsScreenService.realTrack());
    }
}
